package com.example.myapplication.xiaochengxu;

import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.example.myapplication.xiaochengxu.bean.ZhuMianBean;
import com.example.myapplication.xiaochengxu.bean.ZhuMianBean2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author: rongfeng
 * @date: 2022/10/12
 * @description
 */
public class ZhuMian {
    public static void main(String[] args) {
        ZhuMian zhuMian = new ZhuMian();
        zhuMian.init();
        zhuMian.loop();
    }

    ZhuMianJson zhuMianJson = new ZhuMianJson();
    List<TrackList> trackList = new ArrayList<>();

    String albumId = "35642120";
    int maxPage = 6;
    String mTitle =  "大自然声音助眠";

    void init() {
        zhuMianJson.id = 1;
        zhuMianJson.title = mTitle;
        zhuMianJson.trackList = trackList;
    }

    int pageIndex = 0;
    int listIndex = 0;
    List<ZhuMianBean.DataBean.TracksBean> tracks;


    public void loop() {
        if (pageIndex < maxPage) {
            pageIndex++;
            query();
        } else {
            String json = GsonUtil.toJson(zhuMianJson)
                    .replaceAll("https://aod.cos.tx.xmcdn.com/", "")
                    .replaceAll("（声控助眠）", "");
            save(json);
        }
    }

    public void loop2() {
        if (listIndex < tracks.size()) {
            ZhuMianBean.DataBean.TracksBean tracksBean = tracks.get(listIndex);
            query2(tracksBean);
            listIndex++;
        } else {
            listIndex = 0;
            loop();
        }
    }

    void save(String json) {
        try {
            System.out.println(json);
            File file = new File("D:\\wrf\\zhumian\\zhumian.txt");
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();
            System.out.println("保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void query() {
        String url = "https://www.ximalaya.com/revision/album/v1/getTracksList?albumId=" + albumId + "&pageNum=" + pageIndex + "&sort=0";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
//                .addHeader("Accept-Encoding", "gzip, deflate, br")
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                ZhuMianBean bean = GsonUtil.parse(json, ZhuMianBean.class);
                tracks = bean.getData().getTracks();
                loop2();
            }
        });
    }

    public void query2(ZhuMianBean.DataBean.TracksBean tracksBean) {
        String url = "https://www.ximalaya.com/revision/play/v1/audio?id=" + tracksBean.getTrackId() + "&ptype=1";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
//                .addHeader("Accept-Encoding", "gzip, deflate, br")
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                System.out.println(json);
                ZhuMianBean2 bean = GsonUtil.parse(json, ZhuMianBean2.class);
                trackList.add(new TrackList(tracksBean.getTitle(), tracksBean.getDuration(), bean.getData().src));
                loop2();
            }
        });
    }


    static class ZhuMianJson {
        int id;
        String title;
        List<TrackList> trackList;
    }

    static class TrackList {
        String t;
        int d;
        String u;

        public TrackList(String t, int d, String u) {
            this.t = t;
            this.d = d;
            this.u = u;
        }
    }


}
